package com.yang.leather.admin.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(BaseAction.class);
	
	protected String base;
	
	/*用于区分转发路径*/
	protected String language;
	
	public String getBase() {
		return base;
	}

	public void setBase(String base) {
		this.base = this.getRequest().getContextPath();
	}
	
	public String getLanguage() {
		if(StringUtils.isEmpty(this.language)){
			this.language = (String)this.getRequest().getAttribute("language");
		}
		return language;
	}

	public void setLanguage(String language) {
		this.language = (String)this.getRequest().getAttribute("language");
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
	
	/**
	 * 绕过Template,直接输出内容的简便函数.
	 */
	protected String render(String text, String contentType) {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType(contentType);
			response.getWriter().write(text);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 直接输出字符串.
	 */
	protected String renderText(String text) {
		return render(text, "text/plain;charset=UTF-8");
	}

	/**
	 * 直接输出字符串GBK编码.
	 */
	protected String renderHtmlGBK(String html) {
		return render(html, "text/html;charset=UTF-8");
	}



}
